package mBio;

import java.io.*;
import java.util.*;
import rfBio.*;

public class remove_overlaps_psl3 {

	public static void main(String[] args)
		throws IOException, FileNotFoundException{
			
		if(args.length != 4){
			System.out.println("Wrong number of parameters!" + '\n' + "Usage: remove_overlaps_psl3 reference_file new_file out_tag min_score.");
			System.exit(0);
		}

		// local variables
		Vector ref_vector = new Vector(100,100); 
		Vector new_vector = new Vector(100,100); 
		boolean addFlag = true;
		String o_filename = "";
		String no_filename = "";
		String o_filenametab = "";
		String no_filenametab = "";
		psl3 nov = new psl3();
		


		BufferedReader refFile = new BufferedReader ( new FileReader (args[0]));
		
		// read ref_file line by line, store in ref_vector
		while (true){
			String line = refFile.readLine();
			if (line == null)
			{
				break;
			}
			psl3 temp_psl3 = new psl3(line);
			
			if( (temp_psl3.get_score() <= Type.toInt(args[3])) )
			{
				addFlag=false;
			}
			
			if(addFlag){
				for(int i =0; i < ref_vector.size(); i++){
					psl3 tmp = new psl3((psl3) ref_vector.get(i));
					if(temp_psl3.get_Q_name().equals(tmp.get_Q_name())){
						if( (temp_psl3.get_score() >= tmp.get_score())){			
							ref_vector.set(i, temp_psl3);
							addFlag=false;
						}
						else{
							addFlag=false;
						}	
					}
				}
			}

			if(addFlag){
				ref_vector.add(temp_psl3);
			}
			
			addFlag=true;
		} 		
		// close ref_ file
		refFile.close();
		
		BufferedReader newFile = new BufferedReader ( new FileReader (args[1]));
		
		// read new_file line by line, store in ref_vector
		addFlag = true;
		while (true){
			String line = newFile.readLine();
			if (line == null)
			{
				break;
			}
			psl3 temp_psl3 = new psl3(line);
			
			if( (temp_psl3.get_score() <= Type.toInt(args[3])) )
			{
				addFlag=false;
			}
			
			if(addFlag){
				for(int i =0; i < new_vector.size(); i++){
					psl3 tmp = new psl3((psl3) new_vector.get(i));
					if(temp_psl3.get_Q_name().equals(tmp.get_Q_name())){
						if( (temp_psl3.get_score() >= tmp.get_score())){
							//System.out.println("Replaced " + temp_psl3.get_Q_name() + " " + temp_psl3.get_score() + " with " + tmp.get_Q_name() + " " + tmp.get_score() );
							new_vector.set(i, temp_psl3);
							addFlag=false;
						}
						else{
							addFlag=false;
						}	
					}
				}
			}

			if(addFlag){			
				//check if overlaps any in the ref_set and label
				for(int i =0; i < ref_vector.size();i++){
					temp_psl3.overlaps((psl3) ref_vector.get(i));
				}
				new_vector.add(temp_psl3);
			}
			addFlag = true;
		}
			
		newFile.close();
		
		//write output files
		o_filename = args[2] + "_overlaps.txt";
		no_filename = args[2] + "_nonoverlaps.txt";
		o_filenametab = args[2] + "_overlaps.tab";
		no_filenametab = args[2] + "_nonoverlaps.tab";
		
		PrintWriter o_file = new PrintWriter(new BufferedWriter(new FileWriter(o_filename)));
		PrintWriter no_file = new PrintWriter(new BufferedWriter(new FileWriter(no_filename)));
		PrintWriter o_filetab = new PrintWriter(new BufferedWriter(new FileWriter(o_filenametab)));
		PrintWriter no_filetab = new PrintWriter(new BufferedWriter(new FileWriter(no_filenametab)));
		
		for(int i =0; i < new_vector.size(); i++)
		{
			nov = (psl3) new_vector.get(i);
			if(nov.get_overlaps()){
				o_file.println( nov.get_Q_name() + " overlaps with " + nov.get_Overlaps_With());
				o_filetab.println(nov.get_Q_name());
			}
			else{
				no_file.println( nov.get_Q_name() + " does not overlap any sequence in the referens set.");
				no_filetab.println(nov.get_Q_name());
			}
		}
		
		o_file.close();
		no_file.close();
		o_filetab.close();
		no_filetab.close();
   }
}